#include"Date.h"

Date::Date(int year,int month,int day)
{
    if(month>0 && month<13 && day>0 && day<=GetMonthDay(year,month))
    {
        _year=year;
        _month=month;
        _day=day;
    }
    else 
    {
        cout<<"非法初始化"<<endl;
        assert(0);
    }
}

Date:: Date(const Date& date)
{
    _year=date._year;
    _month=date._month;
    _day=date._day;
}

Date& Date::operator=(const Date& date)
{
    if(this!=&date)
    {
        _year=date._year;
        _month=date._month;
        _day=date._day;
    }
    return *this;
}

int Date::GetMonthDay(int year,int month)
{
    static int arr[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    if(month==2 && ((year%4==0 && year%100!=0)||year%400==0))
        return 29;
    return arr[month];
}

void Date::Print()
{
    cout<<_year<<"."<<_month<<"."<<_day<<endl;
}

bool Date::operator==(const Date& date)
{
    if(_year==date._year && _month==date._month && _day==date._day)return true;

    else return false;
}
bool Date::operator>(const Date& date)
{
    if(_year>date._year)return true;
    else if(_year==date._year && _month>date._month)return true;
    else if(_year==date._year && _month==date._month && _day>date._day)return true;
    else return false;
}
bool Date::operator>=(const Date& date)
{
    return *this>date || *this==date;
}
bool Date::operator<(const Date& date)
{
    return !(*this>=date);
}
bool Date::operator<=(const Date& date)
{
    return !(*this>date);
}

Date& Date::operator+=(int day)
{   
    _day+=day;
    while(_day>GetMonthDay(_year,_month))
    {
        _day-=GetMonthDay(_year,_month);
        _month++;

        if(_month>12)
        {
            _year++;
            _month=1;
        }
    }
    return *this;
}
Date Date::operator+(int day)
{
    Date tmp=*this;
    tmp+=day;
    return tmp;
}
Date& Date::operator++()
{
    *this+=1;
    return *this;
}
Date Date::operator++(int)//后置++
{
    Date tmp=*this;
    *this+=1;
    return tmp;
}
Date& Date::operator--()
{
    *this-=1;
    return *this;
}
Date Date::operator--(int)//后置++
{
    Date tmp=*this;
    *this-=1;
    return tmp;
}
Date& Date::operator-=(int day)
{
    while(day>0)
    {
        if(day>=_day)
        {
            day-=_day;
            _month--;
            if(_month==0)
            {
                _year--;
                _month=12;
            }
            _day=GetMonthDay(_year,_month);
        }
        else 
        {
            _day-=day;
            break;
        }
    }
    return *this;
}
Date Date::operator-(int day)
{
    Date tmp=*this;
    tmp-=day;
    return tmp;
}

// Date Date::operator-(Date date)
// {
//     if(*this>date)
//     {
//         int sum_day=0;
//         //先把日期较小的日、月都减到0  记录天数零头
//         sum_day+=date._day;
//         while(date._month>=1)
//         {
//             if(date._month==1)break;

//             date._month--;
//             sum_day+=GetMonthDay(date._year,date._month);
//         }
//         Date tmp=*this-sum_day;
//         //tmp.Print();
//         tmp._year-=date._year,tmp._month-=1;
//         return tmp;
//     }
//     else if(*this<date)
//     {
//         int sum_day=0;
//         Date date_tmp=*this;
//         //先把日期较小的日、月都减到0  记录天数零头
//         sum_day+=date_tmp._day;
//         while(date_tmp._month>=1)
//         {
//             if(date_tmp._month==1)break;

//             date_tmp._month--;
//             sum_day+=GetMonthDay(date_tmp._year,date_tmp._month);
//         }
//         Date tmp=date-sum_day;
//         tmp._year-=date_tmp._year,tmp._month-=1;
//         return tmp;
//     }
//     return Date(0,0,0);
// }

int Date::operator-(Date date)//日期-日期
{
    int ret=0;
    if(*this>date)
    {
        while(*this>date)
        {
            date++;
            ret++;
        }
        return ret;
    }

    Date tmp=*this;
    while(tmp<date)
    {
        tmp++;
        ret++;
    }
    return -ret;
}

// void Date::operator<<(ostream& out)//流提取运算符重载
// {
//     cout<<_year<<"年"<<_month<<"月"<<_day<<"日"<<endl;
// }

ostream& operator<<(ostream& out,const Date& date)//流提取运算符重载
{
    cout<<date._year<<"年"<<date._month<<"月"<<date._day<<"日"<<endl;//在全局声明，函数变无法访问类里面的私有成员变量
    return out;
}
istream& operator>>(istream& in,Date& date)
{
    in>>date._year>>date._month>>date._day;
    if(date._month>0 && date._month<13 && date._day>0 && date._day<=date.GetMonthDay(date._year,date._month))
    {}
    else
    {
        cout<<"非法输入"<<endl;
        assert(0);
    }
    return in;
}